<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$fenlei=pdo_getall('jy_zhongyi_pdf_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title','thumb'),'','paixu desc, id desc');
	$return['url']=custom_url('core/welcome.home/',true);	
	if($fenlei){
		foreach($fenlei as &$item){
			$item['url']=custom_url('/pdf//group_id='.$item['id'],true);
			$item['thumb'] = $item['thumb'] ? $item['thumb'] : '../core/resource/images/nopic.jpg';
		}
	}
	$return['item']['fenleiurl']=custom_url('/pdf//fenlei='.$return['item']['fenlei'],true);
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$group_id=intval($_RQ['group_id'])?:$fenlei[0]['id'];
	if($group_id){
		$params['group_id']=$group_id;
		$return['params']['group_id']=$group_id;
	}
	$return['fenleiname']=pdo_getcolumn('jy_zhongyi_pdf_group',array('id'=>$group_id),'title');
	$order=intval($_RQ['order']);
	if($order==1){
		$order='id desc';
	}elseif($order==2){
		$order='id desc';
	}else{
		$order='id desc';
	}
	$orderby=array();
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_zhongyi_pdf',$params,array($pindex,$psize),$total,$fields,'',$order);
	$nums=36;
	if($list){
		foreach($list as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,$nums);
			$item['thumb'] = $item['thumb'] ? $item['thumb'] : '../core/resource/images/nopic.jpg';
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['fenlei']=$fenlei[$item['fenlei']]['title'];
			$item['url']=custom_url('/pdf/detail/id='.$item['id'],true);
		}
	}
	$sharelink='/pdf/list/fromuser='.$_SESSION['uid'].'&group_id='.$group_id;
	$return['sharedata']=array(
			'title'=>$_RQ['group_id'] ? $fenlei[$group_id]['title'] . '-' . $_SESSION['account']['title']: '文章分类 -'.$_SESSION['account']['title'],
			'desc'=>$_SESSION['account']['title'].'-资料列表',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($return['account']['logo']),
	);
	$hotlist=$list;
	paixu($hotlist,'re_readnum','desc');
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['group_id']=$group_id?: 0;
	$return['groups']=$fenlei?$fenlei:array();
	$return['list']=$list?$list:array();
	$return['hotlist']=$hotlist?$hotlist:array();
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	set_html('title',$return['fenleiname'].'-'.$_SESSION['account']['title']);
	exi($return);
}
if($_RQ['op']=='detail'){
	//刷新阅读次数
	$pdf_id=intval($_RQ['id']);
	$return['item']=pdo_get('jy_zhongyi_pdf',array('id'=>$pdf_id,'uniacid'=>$_SESSION['uniacid']));
	if(!$return['item']){
		exi('指定资料不存在！','error','list');
	}	
	$return['group_id']=$return['item']['fenlei'];
	//点赞数=虚拟点赞数+真实点赞数
	$return['item']['likenum']+=pdo_count('jy_zhongyi_pdf_like',array('pdf_id'=>$pdf_id,'like'=>1));
	//unset($return['item']['re_likenum']);
	$return['item']['advs']=json_decode($return['item']['advs'],true);
	$return['item']['createtime']=date('20y-m-d',$return['item']['createtime']);
	$return['groups']=pdo_getall('jy_zhongyi_pdf_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['item']['fenleiurl']=custom_url('/pdf//id='.$return['item']['fenlei'],true);
	$return['item']['fenlei']=$return['fenlei'][$return['item']['fenlei']]['title'];
	//去掉多余空样式
	$return['item']['content']=str_replace('style=""','',$return['item']['content']);
	//$return['item']['content'] = str_replace('&nbsp;','',$return['item']['content']);
	$content=strip_tags($return['item']['content']);
	$return['item']['desc'] = $return['item']['desc'] ? $return['item']['desc'] : cfc('tools')->substr($content,0,36);
	$sharelink='/pdf/detail/fromuser='.$_SESSION['uid'].'&id='.$return['item']['id'];
	$return['sharedata']=array(
			'title'=>$return['item']['title'] . '-' . $_SESSION['account']['title'],
			'desc'=>$return['item']['desc'],
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>$return['item']['thumb']?tomedia($return['item']['thumb']):tomedia($return['account']['logo']),
	);
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fenleiid=intval($_RQ['group_id']);
	if($fenleiid){
		$params['fenlei']=$fenleiid;
	}
	$orderby=array();
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_zhongyi_pdf',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['desc2']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,36);
			$item['thumb'] = $item['thumb'] ? $item['thumb'] : '../core/resource/images/nopic.jpg';
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['fenlei']=$fenlei[$item['fenlei']]['title'];
			$item['url']=custom_url('/pdf/detail/id='.$item['id'],true);
		}
	}
	$return['list']=$list?$list:array();
	set_html('title',$return['item']['title']);
	exi($return);
}